package com.zyk.leetcode;

import java.util.Comparator;

/**
 * @author zhangsan
 * @date 2021/4/12 8:58
 */
public class C179 {

    Comparator<Integer> comparator = (x, y) -> {
        long sx = 10, sy = 10;
        while (sx <= x) sx *= 10;
        while (sy <= y) sy *= 10;
        return (int) (-sy * x - y + sx * y + x);
    };

    // https://leetcode-cn.com/problems/largest-number/
    public String largestNumber(int[] nums) {
        insertionSort(nums);
        StringBuilder sb = new StringBuilder();
        if (nums[0] == 0) return String.valueOf(0);
        for (int num : nums) sb.append(num);
        return sb.toString();
    }

    public void insertionSort(int[] arr) {
        for (int i = 1; i < arr.length; i++)
            for (int j = i; j > 0 && comparator.compare(arr[j], arr[j - 1]) < 0; j--) swap(arr, j, j - 1);

    }

    public void swap(int[] arr, int a, int b) {
        arr[a] = arr[a] ^ arr[b]; arr[b] = arr[a] ^ arr[b]; arr[a] = arr[a] ^ arr[b];
    }


    public static void main(String[] args) {
        C179 test = new C179();


        int[] nums = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
        System.out.println(test.largestNumber(nums));
    }

}
